var app = getApp();
const WXAPI = require('../../utils/main')

Component({
  //一些组件选项
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },

  //组件的对外属性，属性设置中可包含三个字段,type 表示属性类型、 value 表示属性初始值、 observer 表示属性值被更改时的响应函数    
  properties: {

    // 弹框显示
    show2: {
      type: Boolean,
      value: '',
      observer(newVal, oldVal) {
        console.log(newVal)
      }
    },
    // 余额支付/余额支付(0/2)
    payType: {
      type: String,
      value: '',
      observer(newVal, oldVal) {
        // console.log(newVal)
        this.balance()
      }
    },
    // 订单号
    ordersn:{
      type: String,
      value: '',
    },
    // 金额
    total:{
      type: Number,
      value: '',
      observer(newVal, oldVal) {
        // console.log(newVal)
      }
    },
    // 余额
    balance:{
      type: Number,
      value: '',
      observer(newVal, oldVal) {
        console.log(newVal)
        this.balance()
      }
    },
    // 判断跳我的页面还是返回页
    judge:{
      type: Boolean,
      value: '',
      observer(newVal, oldVal) {
        console.log(newVal)
      }
    },
  },
  //组件的内部数据，和 properties 一同用于组件的模版渲染
  data:{

  },
  //组件的方法，包括事件响应函数和任意的自定义方法 
  methods: {

  hideBuyModal:function(){
    this.triggerEvent('signEvent', {
      'show2':false
    })
    if(this.data.judge){
      setTimeout(() => {
        wx.navigateTo({
            url: "../stay-payment/stay-payment",
        });
      }, 1500);
    }
  },
  switch:function(e){
    console.log(e)
    if(e.currentTarget.dataset.type == 'balance'){
      this.setData({
        payType:"0"
      })
    }else{
      this.setData({
        payType:"2"
      })
    }
  },
  balance:function(e){
    if (this.data.balance < this.data.total){
        this.setData({
          payType:'2'
        })
    }
  },
  //进入支付状态
  payment:function(e){
    let that = this
    console.log(wx.getStorageSync('openid'))
    console.log(wx.getStorageSync('uid'))
    console.log(that.data.total)
    console.log(that.data.ordersn)
      if (that.data.payType == "0"){
        wx.showModal({
          title: '是否点击支付?',
          content: '',
          showCancel: true,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: async(result) => {
              if (result.confirm) {
                that.balance_payment();
              }else if(result.cancel) {
                console.log('用户点击取消')
                that.hideBuyModal()
                return;
              }
          }
        });
      }else if(that.data.payType == "2") {
        console.log('进入微信支付')
        that.wechat_payment();
      }
    },
    //余额支付
    balance_payment:function(){
      console.log(this.data.judge)
      let that = this
      WXAPI.payment({  
        _uid:wx.getStorageSync('uid'),
        _price:that.data.total,
        _ordersn:that.data.ordersn,
        _balance:that.data.balance
      }).then(function (res) {
        console.log(res)
        wx.showToast({
          title: "支付成功",
        });
        if(that.data.judge){
          setTimeout(() => {
            wx.switchTab({
                url: "../my/my",
            });
          }, 1500);
        }else{
          that.hideBuyModal()
          return;
        }
      }).catch(function (e) {
        console.log(e)
        wx.showToast({
          title: "支付失败",
          icon: "none",
        });
      })
    },
    //微信支付
    wechat_payment:function(){
      let that = this
      console.log(that.data.total)
      console.log(that.data.ordersn)
      WXAPI.payment({  
        _uid:wx.getStorageSync('uid'),
        _price:that.data.total,
        _ordersn:that.data.ordersn
      }).then(function (res) {
        console.log(res)
          wx.requestPayment({
            timeStamp: res.timeStamp, //时间戳
            nonceStr: res.nonceStr, //随机字符串
            package: res.package, //统一下单接口返回的 prepay_id 参数值
            signType: res.signType, //签名算法
            paySign: res.paySign, //签名
            success: function(res) {
                console.log(res, "支付成功");
                wx.showToast({
                    title: "支付成功",
                });
                setTimeout(() => {
                  wx.switchTab({
                      url: "../my/my",
                  });
                }, 2000);
            },
            fail: function(err) {
                console.log(err, "支付失败");
                wx.showToast({
                    title: "支付失败",
                    icon: "none",
                });
                that.hideBuyModal()
            },
          });
      }).catch(function (e) {
        console.log(e)
      })
    }
  },
  // 组件生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function(){
  
  },
  moved: function(){},
  detached: function(){},
  pageLifetimes: {
    show: function() {
      // 页面被展示
    },
  }
})
